<?php
//
defined('BY_JYA') or exit('error');
$userid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if(!$_SESSION['uid']){
	exi('无权访问','error');
}
if($_RQ['op']=='main'){
	if(isset($_RQ['status']) && $_RQ['status']!='all'){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']='all';
	}
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='list'){
	//$post=get_postdata();
	$params=array('uniacid'=>$_SESSION['uniacid'],'userid'=>$userid);
	if(isset($_RQ['status']) && $_RQ['status']!='all'){
		$params['status']=$_RQ['status']!=2?$_RQ['status']:array(2,3);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']='all';
	}
	if($_RQ['ispay']){
		$params['ispay']=$_RQ['ispay'];
		$return['params']['ispay']=$_RQ['ispay'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$return['list']=pdo_getslice('jy_jd_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($return['list'] as &$v){
		$v['hotel']=pdo_get('jy_jd_hotel',array('id'=>$v['hotelid']));
		$v['room']=pdo_get('jy_jd_room',array('id'=>$v['roomid']));
		$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
		$v['statustitle']=$this->order_status[$v['status']];
	}
	exi($return);
}

if($_RQ['op']=='del'){
	exi('商户暂时不支持删除订单！','error');
	if(!$_RQ['id']){
		exi('请先选择订单','error');
	}
	$order=pdo_get('jy_jd_order',array('id'=>$_RQ['id']));
	if($order['status']!=5){
		exi('只有已完成的订单可以删除','error');
	}
	pdo_delete('jy_jd_order',array('id'=>$_RQ['id']));
	exi('删除成功','','/manage.order_list/main/');
}
if($_RQ['op']=='queren'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));
		if($order['status']!=0 || $order['ispay']!=1){
			exi('只有预定状态并且已支付的订单才能确认','error','list');	
		}
		pdo_update('jy_jd_order',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$this->model('notice')->notice_querenorder($order);
		exi('确认成功','','/manage.order_list/main/');			
}
if($_RQ['op']=='quxiao'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_jd_order',array('id'=>$id));	
	if($order['status']!=2){
		exi('只有申请取消的订单才能确认取消','error');	
	}
	pdo_update('jy_jd_order',array('status'=>3),array('id'=>$id));
	exi('取消成功');		
}
if($_RQ['op']=='tuifang'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));		
		if($order['status']!=6){
			exi('已入住的订单才能完成退房！','error');	
		}
		pdo_update('jy_jd_order',array('status'=>5),array('id'=>$id));
		$this->model('notice')->notice_tuifang($order);
		exi('退房成功','','/manage.order_list/main/');			
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->model('refund')->refund_pay($id);
		exi('打款成功！','','/manage.order_list/main/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_jd_order',array('id'=>$id));
	if(!$item){
		exi('指定退款订单不存在~','error');
	}
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	if($member['from_os']==1){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('id'=>$item['wxgid']),'title');
	}
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'copy','name'=>'fee','title'=>'退款金额'),
			array('type'=>'copy','name'=>'fee','title'=>'退款到账金额'),
		),
	);
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('请先选择订单','error');
	}
	$order=pdo_get('jy_jd_order',array('id'=>$_RQ['id']));
	if($order['status']!=0 && $order['status']!=1){
		exi('当前订单不可取消','error');
	}
	$room=pdo_get('jy_jd_room',array('id'=>$order['roomid']));
	if(!$room['is_opentui']){
		//exi('该房间不支持退订！','error');
	}
	if($room['tui_day'] || $room['tui_day']){
		$intime=strtotime($order['intime']);
		$tui_daytime=$intime-$room['tui_day']*24*3600;
		$tui_day=date('Y-m-d',$tui_daytime);
		$tui_daytime2=strtotime($tui_day.' '.$room['tui_time']);
		if($tui_daytime2<time()){
			//exi('退订时间已过！','error');
		}
	}
	$this->model('notice')->notice_receiveorder($order);
	if($order['ispay']==1){
		$this->model('refund')->check_order_rerefund($order['id']);
		pdo_update('jy_jd_order',array('status'=>2),array('id'=>$_RQ['id']));
		$this->model('notice')->notice_receiveorder2admin($order);
		exi('已申请取消','','/manage.order_list/main/');
	}
	if($order['ispay']==0){
		pdo_update('jy_jd_order',array('status'=>7),array('id'=>$_RQ['id']));
		exi('已取消','','/manage.order_list/main/');
	}
}

if($_RQ['op']=='ruzhu'){
		$id=intval($_RQ['id']);
		$order=pdo_get('jy_jd_order',array('id'=>$id));		
		if($order['status']!=1){
			exi('已预定成功的订单才能入住！','error');	
		}
		pdo_update('jy_jd_order',array('status'=>6),array('id'=>$id));
		$this->model('notice')->notice_ruzhu($order);
		exi('入住成功','','/manage.order_list/main/');			
}
?>